require 'net/smtp'
require 'net/http'

#some variables to set
emailUsername = ''
emailPassword = ''
breweryUrl = 'http://surlybrewing.com/tours/tourregistration.php'

#weird syntax in ruby to create a block of text (called a 'here document') and also sets the subject for the email
msgstr = <<END_OF_MESSAGE
Subject: Surly tour registration open

http://surlybrewing.com/brewery/surly-brewery-tours-form.html
END_OF_MESSAGE

uri = URI(breweryUrl)
emailSent = false

#loop until the email sends
while !emailSent do 
	breweryResponse = Net::HTTP.get(uri)
	if(!/All tours are full/.match(breweryResponse))
		smtp = Net::SMTP.new 'smtp.gmail.com', 587
		smtp.enable_starttls
		smtp.start('gmail.com',emailUsername,emailPassword,:login)do |smtp|
			smtp.send_message(msgstr,'email@gmail.com','email@gmail.com')
			emailSent = true
		end
	else
		#get a random time betwee 1.5 minutes and 10 minutes
		r = Random.new
		waitTime = r.rand(90..600)
		puts "Tours are full...trying again in #{waitTime} seconds"
		sleep(waitTime)
	end
end



























